package cn.com.dhc.danlu.fragment.enveloplogic;

import android.content.Context;
import android.text.TextUtils;
import android.widget.BaseAdapter;

import java.util.List;

import cn.com.dhc.danlu.util.CommonUtils;
import cn.com.dhc.danlu.widget.adapter.EnvelopeableAdapter;
import cn.com.dhc.danlu.widget.base.BaseListViewLogic;
import cn.com.dhc.danlu.widget.loadlayer.LoaddingLayout;
import cn.com.dhc.mibd.eufw.widget.android.ptr.CustomPullToRefreshListView;
import cn.com.hd.mall.web.webservices.entity.request.envelop.GetCouponListRequest;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetCouponListResponse;
import cn.com.hd.mall.web.webservices.entity.response.cpx.GetCouponListResponse.CouponListModel;

/**
 * @author wuhaiyang
 */
public class EnvelopeablePresenter extends BaseListViewLogic<GetCouponListRequest, GetCouponListResponse> {
    /**
     * 可用优惠券和不可用优惠券的flag
     */
    private int mStatus;
    private boolean isEmpty = false;


    //    public EnvelopeablePresenter(AsyncTaskActivityDelegate delegate, CustomPullToRefreshListView ptrListView, String jsonStr, int type) {
    //        super(delegate, ptrListView);
    //        this.mStatus = type;
    //
    //        if (!TextUtils.isEmpty(jsonStr)) {
    //            GetCouponListResponse data = CommonUtils.parseJson2Bean(jsonStr, GetCouponListResponse.class);
    //            List<CouponListModel> list;
    //            if (mStatus == 0) {
    //                list = data.getUseablecouponList();
    //            } else {
    //                list = data.getUnUseablecouponList();
    //            }
    //            if (null == list || list.isEmpty()) {
    //                isEmpty = true;
    //            }
    //            mAdapter.refreshData(list);
    //        } else {
    //            isEmpty = true;
    //        }
    //        ((EnvelopeableAdapter) mAdapter).refreshType(type);
    //    }
    public EnvelopeablePresenter(Context context, CustomPullToRefreshListView ptrListView, String jsonStr, int type) {
        super(context, ptrListView);
        this.mStatus = type;

        if (!TextUtils.isEmpty(jsonStr)) {
            GetCouponListResponse data = CommonUtils.parseJson2Bean(jsonStr, GetCouponListResponse.class);
            List<CouponListModel> list;
            if (mStatus == 0) {
                list = data.getUseablecouponList();
            } else {
                list = data.getUnUseablecouponList();
            }
            if (null == list || list.isEmpty()) {
                isEmpty = true;
            }
            mAdapter.refreshData(list);
        } else {
            isEmpty = true;
        }
        ((EnvelopeableAdapter) mAdapter).refreshType(type);
    }

    public void showHistorySelect(List datas) {
        if (null != datas) {
            ((EnvelopeableAdapter) mAdapter).showHistory(datas);
        }
    }

    @Override
    protected BaseAdapter initAdapter() {
        // mStatus == 0  当前构造方法还未执行完
        return new EnvelopeableAdapter(getContext(), mStatus);
    }

    @Override
    public void setLoaddingLayout(LoaddingLayout mLoaddingLayout) {
        super.setLoaddingLayout(mLoaddingLayout);
        mLoaddingLayout.changeEmptyViewTips(mStatus == 0 ? "暂无可用优惠券" : "暂无不可用优惠券");
        mLoaddingLayout.isHidenInteraction(true);
        if (isEmpty) {
            mLoaddingLayout.showEmptyView();
        } else {
            mLoaddingLayout.showContentView();
        }
    }

    @Override
    public void callFetchData() {

    }

    /**
     * 以下方法暂时未使用 因为数据是从上一个界面传递过来
     */
    @Override
    protected List getList(GetCouponListResponse model) {
        return null;
    }

    public Object getModelByPos(int index) {
        if (null != mAdapter) {
            return mAdapter.getItem(index);
        }
        return null;
    }
}